package com.jinzhi.jzweb.controller;


import java.util.*;
import java.util.stream.Collectors;

import com.jinzhi.common.utils.StringUtil;
import com.jinzhi.jzweb.domain.JazSysProjectDO;
import com.jinzhi.jzweb.service.JazSysProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.JazSysProjectTypeDO;
import com.jinzhi.jzweb.service.JazSysProjectTypeService;
import com.jinzhi.common.utils.Result;

/**
 * 
 * <pre>
 * 服务项目内容表
 * </pre>
 * <small> 2024-05-24 17:24:31 | Long</small>
 */
@Controller
@RequestMapping("/jzweb/jazSysProjectType")
public class JazSysProjectTypeController extends BaseController {
	@Autowired
	private JazSysProjectTypeService jazSysProjectTypeService;

	@Autowired
	private JazSysProjectService jazSysProjectService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:jazSysProjectType:jazSysProjectType")
	String JazSysProjectType(){
	    return "jzweb/jazSysProjectType/jazSysProjectType";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<JazSysProjectTypeDO>> list(JazSysProjectTypeDO jazSysProjectTypeDTO){
		// 查询列表数据
		Page<JazSysProjectTypeDO> page = jazSysProjectTypeService.selectPage(getPage(JazSysProjectTypeDO.class),
					jazSysProjectTypeService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				JazSysProjectTypeDO obj = (JazSysProjectTypeDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}

	@ResponseBody
	@GetMapping("/listType")
	public Result<List<JazSysProjectTypeDO>> listType(JazSysProjectTypeDO jazSysProjectTypeDTO){
		// 查询列表数据
		Wrapper<JazSysProjectTypeDO> wrapper = new EntityWrapper<JazSysProjectTypeDO>()
				.eq("sys_project_id", jazSysProjectTypeDTO.getSysProjectId())
				.orderBy("create_time",true);
		List<JazSysProjectTypeDO> jazSysProjectTypeDOS = jazSysProjectTypeService.selectList(wrapper);
		jazSysProjectTypeDOS.forEach(jazSysProjectTypeDO -> {
			if(jazSysProjectTypeDO.getSysProjectId()!=null){
				JazSysProjectDO jazSysProjectDO = jazSysProjectService.selectById(jazSysProjectTypeDO.getSysProjectId());
				jazSysProjectTypeDO.setSysProjectName(jazSysProjectDO.getName());
			}
		});
		return Result.ok(jazSysProjectTypeDOS);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:jazSysProjectType:add")
	String add(){
	    return "jzweb/jazSysProjectType/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:jazSysProjectType:edit")
	String edit(@PathVariable("id") Long id,Model model){
		JazSysProjectTypeDO jazSysProjectType = jazSysProjectTypeService.selectById(id);
		model.addAttribute("jazSysProjectType", jazSysProjectType);
	    return "jzweb/jazSysProjectType/edit";
	}
	
	@Log(value = "添加服务项目内容表",sysModule = 10, operationType = 1)
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save(@RequestBody JazSysProjectTypeDO jazSysProjectType){
		jazSysProjectTypeService.insert(jazSysProjectType);
        return Result.ok();
	}
	
	@Log(value = "修改服务项目内容表",sysModule = 10, operationType = 3)
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update(@RequestBody JazSysProjectTypeDO jazSysProjectType){
		JazSysProjectTypeDO jazSysProjectTypeDO = jazSysProjectTypeService.selectById(jazSysProjectType.getId());
		if (jazSysProjectTypeDO==null){
			return Result.fail();
		}
		jazSysProjectTypeDO.setWorkType(jazSysProjectType.getWorkType());
		boolean update = jazSysProjectTypeService.updateById(jazSysProjectType);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log(value = "删除服务项目内容表",sysModule = 10, operationType = 2)
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove(Long id){
		boolean delete = jazSysProjectTypeService.deleteById(id);
		return delete ? Result.ok() : Result.fail();
	}
	
}
